package com.easy.common.util.mail;

import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import sun.misc.BASE64Encoder;

import com.easy.common.util.file.FileUtility;

@SuppressWarnings("restriction")
public class SimpleMailSender
{
	public void sendTextMail(MailSenderInfo mailInfo)
	throws Exception
	{
		Message mailMessage = buildMessage(mailInfo);
		mailMessage.setText(mailInfo.getContent());

		Transport.send(mailMessage);
	}

	public void sendHtmlAndAttachMail(MailSenderInfo mailInfo)
	throws Exception
	{
		Message mailMessage = buildMessage(mailInfo);
		Multipart mainPart = buildMailPart(mailInfo.getContent(), mailInfo.getAttachFileNames());

		mailMessage.setContent(mainPart);
		Transport.send(mailMessage);
	}

	private Multipart buildMailPart(String bodyStr, String[] attachs)
	throws Exception
	{
		Multipart mainPart = new MimeMultipart();

		BodyPart html = new MimeBodyPart();

		html.setContent("<meta http-equiv=Content-Type content=text/html; charset=UTF-8>" + bodyStr, "text/html; charset=utf-8");
		mainPart.addBodyPart(html);

		if (attachs == null) {
			return mainPart;
		}
		for (int i = 0; i < attachs.length; i++) {
			BodyPart filePart = buildAttachPart(attachs[i]);
			if (filePart != null) {
				mainPart.addBodyPart(filePart);
			}
		}
		return mainPart;
	}

	private BodyPart buildAttachPart(String filePath) throws Exception {
		File attachFile = new File(filePath);
		if ((!attachFile.isFile()) || (!attachFile.exists())) {
			return null;
		}
		BodyPart bp = new MimeBodyPart();
		FileDataSource fileds = new FileDataSource(attachFile);
		bp.setDataHandler(new DataHandler(fileds));
		BASE64Encoder enc = new BASE64Encoder();
		String fileName = FileUtility.getFileName(filePath);
		bp.setFileName("=?UTF-8?B?" + enc.encode(fileName.getBytes("UTF-8")) + "?=");

		return bp;
	}

	private Message buildMessage(MailSenderInfo mailInfo)
	throws Exception
	{
		MailAuthenticator authenticator = null;
		Properties pro = mailInfo.getProperties();

		if (mailInfo.isValidate()) {
			authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
		}
		Session sendMailSession = Session.getInstance(pro, authenticator);

		Message mailMessage = new MimeMessage(sendMailSession);

		Address from = new InternetAddress(mailInfo.getFromAddress());

		mailMessage.setFrom(from);

		Address to = new InternetAddress(mailInfo.getToAddress());

		mailMessage.setRecipient(Message.RecipientType.TO, to);

		mailMessage.setSubject(mailInfo.getSubject());

		mailMessage.setSentDate(new Date());
		return mailMessage;
	}
}